public class L112 {

    public static void main(String[] args) {

    }

    class Solution {
        public boolean hasPathSum(TreeNode root, int targetSum) {
            if (root == null) {
                return false;
            }
            return path(root, targetSum);

        }

        private boolean path(TreeNode root, int targetSum) {
            targetSum -= root.val;
            if (root.left == null && root.right == null) {
                return targetSum == 0;
            }
            if (root.left != null) {
                boolean left = path(root.left, targetSum);
                if (left) {
                    return true;
                }
            }
            if (root.right != null) {
                boolean right = path(root.right, targetSum);
                if (right) {
                    return true;
                }
            }
            return false;
        }
    }
}
